/**
 * 
 */
package com.storm.lib.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.storm.lib.base.BasePo;
import com.storm.lib.base.BaseService;
import com.storm.lib.base.BaseTimer;
import com.storm.lib.dao.TimerDAO;
import com.storm.lib.po.TimerPo;
import com.storm.lib.type.LogType;
import com.storm.lib.type.TimerType;
import com.storm.lib.util.ExceptionUtil;
import com.storm.lib.util.ObjectUtil;
import com.storm.lib.util.ParameterUtil;



/**
 * 类功能: 时间服务类
 * @author johnny
 * @version 2010-12-1
 */
@Service
public class TimerService extends BaseService{
	@Autowired
	private TimerDAO timerDAO;

	/**
	 * 方法功能:执行Timer
	 * 更新时间:2010-12-1, 作者:johnny
	 * @param timerId
	 */
	public void executeTimer(int timerId) {
		TimerPo timerPo = (TimerPo) timerDAO.getEntityPo(timerId, TimerPo.class);
		if(timerPo==null){
			return;
		}
		if(timerPo.getState()!=TimerType.TIMER_STATE_WAIT){
			ExceptionUtil.throwInvalidParamException();
		}
		BaseTimer baseTimer = getTimerByType(timerPo.getTimerType());
		baseTimer.baseParameter = ParameterUtil.getParameter(timerPo.getParameters());
		baseTimer.execute();
		finishTimer(timerPo);
	}

	/**
	 * 方法功能:完成TimerPo
	 * 更新时间:2010-12-1, 作者:johnny
	 * @param timerPo
	 */
	private void finishTimer(TimerPo timerPo) {
		timerPo.setState(TimerType.TIMER_STATE_FINISHED);
		timerDAO.updateEntity(timerPo);
//		timerDAO.updateEntityProperties(timerPo, "state", TimerType.TIMER_STATE_FINISHED);
	}
	/**
	 * 方法功能:根据type获得Timer
	 * 更新时间:2010-12-1, 作者:johnny
	 * @param timerType
	 * @return
	 */
	public BaseTimer getTimerByType(int timerType) {
		Class clazz =TimerType.MAPPING_TIMER_TYPE.get(timerType);
		if(clazz==null){
			ExceptionUtil.throwInvalidParamException();
		}
		return (BaseTimer) ObjectUtil.getNewInstance(clazz);
	}

	/**
	 * 方法功能:创建定时器
	 * 更新时间:2010-12-1, 作者:johnny
	 * @param timerPo
	 * @return
	 */
	public BasePo createTimer(TimerPo timerPo) {
		return timerDAO.insert(timerPo);
	}

	/**
	 * 方法功能:找到Timer
	 * 更新时间:2011-1-11, 作者:johnny
	 * @param timerId
	 * @return
	 */
	public TimerPo findTimer(Integer timerId) {
		TimerPo timerPo = (TimerPo) timerDAO.getEntityPo(timerId, TimerPo.class);
		return timerPo;
	}
	
	/**
	 * 从数据库删除timer
	 */
	public void delTimerFromDB(){
		String countSql = "SELECT count(1) FROM {#USER_DB}.po_timer WHERE state = "+LogType.TIMER_TYPE;
		String sql = "DELETE FROM {#USER_DB}.po_timer WHERE state = "+LogType.TIMER_TYPE;
		long count = timerDAO.queryIntForSql(countSql);
		if(count>LogType.DEL_MAX_COUNT){
			count = count/LogType.DEL_MAX_COUNT;
			count = count%LogType.DEL_MAX_COUNT == 0 ? count : count + 1;
			for(int i=0; i<count; i++){
				sql = sql + "limit " + LogType.DEL_MAX_COUNT;
				timerDAO.executeSql(sql);
			}
		}else{
			timerDAO.executeSql(sql);
		}
		
	}

	public void syncTimer(TimerPo timerPo) {
		timerDAO.syncToDB(timerPo);
	}
}
